import localForage from 'localforage'

const localOperates = {
  getItem(key: string) {
    return new Promise<any>((resolve, reject) => {
      localForage
        .getItem(key)
        .then((value) => {
          resolve(value)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  setItem(key: string, value: any) {
    return new Promise<any>((resolve, reject) => {
      localForage
        .setItem(key, value)
        .then((value) => {
          resolve(value)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  removeItem(key: string) {
    return new Promise<boolean>((resolve, reject) => {
      localForage
        .removeItem(key)
        .then(() => {
          resolve(true)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  clear() {
    return new Promise<boolean>((resolve, reject) => {
      localForage
        .clear()
        .then(() => {
          resolve(true)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
}

export default localOperates
